/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bsc.war.populate;

import bsc.ejb.entity.*;
import bsc.ejb.entity.ScoreCardDimension;
import bsc.ejb.metadata.Comparation;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.service.ScoreCardUtils;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author luisZavaleta
 */
public class AdvancePopulate extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

        //Create Map Scorecard
        Map mapScorecard = (Map) Factory.getBean("Map");
        mapScorecard.setName("Mapa de Finanzas");
        delegate.createMap(mapScorecard);

        //Create Perspective Scorecard
        Perspective perspectiveScorecard = (Perspective) Factory.getBean("Perspective");
        perspectiveScorecard.setName("Perspectiva de Finanzas");
        delegate.createPerspective(perspectiveScorecard);

        //Create Objetive Scorecard
        Objective objectiveScorecard = (Objective) Factory.getBean("Objective");
        objectiveScorecard.setName("F1. Mejorar las utilidades de la empresa");
        delegate.createObjective(objectiveScorecard);








        //Create Perspective Scorecard

        //Settings of the KPI
        Settings setting = (Settings) Factory.getBean("Settings");
        setting.setType(Settings.Type.SCORECARD);
        setting.setMathOperation("C/P");

        //Populate a new KPI
        Pointer pointer = (Pointer) Factory.getBean("Pointer");
        pointer.setName("F1.1 Ventas Netas");
        pointer.setDescription("Indicador de prueba");



        delegate.createPointer(pointer);

        //Populate a new Dimension
        Dimension dimension = (Dimension) Factory.getBean("Dimension");
        dimension.setName("Region");
        delegate.createDimension(dimension);

        //Populate a new Member
        DimensionMember dimensionmember1 = (DimensionMember) Factory.getBean("DimensionMember");
        dimensionmember1.setName("TOTAL_REGION");
        dimensionmember1.setDimension(dimension);
        delegate.createDimensionMember(dimensionmember1);

        DimensionMember dimensionmember2 = (DimensionMember) Factory.getBean("DimensionMember");
        dimensionmember2.setName("MX");
        dimensionmember2.setDimension(dimension);
        dimensionmember2.setDimensionMember(dimensionmember1);
        delegate.createDimensionMember(dimensionmember2);

        DimensionMember dimensionmember3 = (DimensionMember) Factory.getBean("DimensionMember");
        dimensionmember3.setName("CR");
        dimensionmember3.setDimension(dimension);
        dimensionmember3.setDimensionMember(dimensionmember1);
        delegate.createDimensionMember(dimensionmember3);


        //Populate a new IndDim
        ScoreCardDimension scorecarddimension = (ScoreCardDimension) Factory.getBean("ScoreCardDimension");
        scorecarddimension.setScoreCard(pointer);
        scorecarddimension.setDimension(dimension);
        delegate.createScoreCardDimension(scorecarddimension);

        //CreateComparation
        Comparation comparationReal = (Comparation) Factory.getBean("Comparation");
        comparationReal.setName("Real");
        comparationReal.setType(Comparation.Type.REAL);

        Comparation comparationGoal = (Comparation) Factory.getBean("Comparation");
        comparationGoal.setName("Meta");
        comparationGoal.setType(Comparation.Type.GOAL);

        delegate.createComparation(comparationReal);
        delegate.createComparation(comparationGoal);


        //Populate a new Comparador Real
        Comparator comparatorR = (Comparator) Factory.getBean("Comparator");
        comparatorR.setDescription("Real");
        comparatorR.setComparation(comparationReal);
        comparatorR.setPointer(pointer);
        delegate.createComparator(comparatorR);

        //Populate a new Comparador Meta
        Comparator comparatorM = (Comparator) Factory.getBean("Comparator");
        comparatorM.setDescription("Meta");
        comparatorM.setComparation(comparationGoal);
        comparatorM.setPointer(pointer);
        delegate.createComparator(comparatorM);

        //Populate DimensionalCut
        DimensionalCut dimensionalcut1 = (DimensionalCut) Factory.getBean("DimensionalCut");
        dimensionalcut1.setPointer(pointer);
        dimensionalcut1.setType(DimensionalCut.Type.TOTAL);
        delegate.createDimensionalCut(dimensionalcut1);

        DimensionalCut dimensionalcut2 = (DimensionalCut) Factory.getBean("DimensionalCut");
        dimensionalcut2.setPointer(pointer);
        dimensionalcut2.setType(DimensionalCut.Type.STANDARD);
        delegate.createDimensionalCut(dimensionalcut2);

        DimensionalCut dimensionalcut3 = (DimensionalCut) Factory.getBean("DimensionalCut");
        dimensionalcut3.setPointer(pointer);
        dimensionalcut3.setType(DimensionalCut.Type.STANDARD);
        delegate.createDimensionalCut(dimensionalcut3);

        //Populate DimensionalCut-DimensionalMember
        DimensionalCutMember dcm = (DimensionalCutMember) Factory.getBean("DimensionalCutMember");
        dcm.setDimensionMember(dimensionmember1);
        dcm.setDimensionalCut(dimensionalcut1);
        delegate.createDimensionalCutMember(dcm);

        DimensionalCutMember dcm2 = (DimensionalCutMember) Factory.getBean("DimensionalCutMember");
        dcm2.setDimensionMember(dimensionmember2);
        dcm2.setDimensionalCut(dimensionalcut2);
        delegate.createDimensionalCutMember(dcm2);

        DimensionalCutMember dcm3 = (DimensionalCutMember) Factory.getBean("DimensionalCutMember");
        dcm3.setDimensionMember(dimensionmember3);
        dcm3.setDimensionalCut(dimensionalcut3);
        delegate.createDimensionalCutMember(dcm3);

        //Time to EA
        DateFormat dfm = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        TimeZone timeZone = calendar.getTimeZone();
        dfm.setTimeZone(timeZone);

        //Se obtiene la fecha de Session
        List<Date> dates = ScoreCardUtils.generateDates(ScoreCardUtils.getSystemDate(request),1);
        for (Date date : dates) {
            Double realRoot= 0D;
            Double goalRoot= 0D;

            //Populate ComparatorDimensionalCut - MX
            ComparatorDimensionalCut cdc5 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
            cdc5.setComparator(comparatorR);
            cdc5.setDimensionalCut(dimensionalcut2);
            cdc5.setRegisterDate(date);
            Double num=ScoreCardUtils.createRandomNumber();
            realRoot+=num;
            cdc5.setRegister(num);
            delegate.createComparatorDimensionalCut(cdc5);

            ComparatorDimensionalCut cdc6 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
            cdc6.setComparator(comparatorM);
            cdc6.setDimensionalCut(dimensionalcut2);
            cdc6.setRegisterDate(date);
            num=ScoreCardUtils.createRandomNumber();
            goalRoot+=num;
            cdc6.setRegister(num);
            delegate.createComparatorDimensionalCut(cdc6);

            //Populate ComparatorDimensionalCut - CR
            ComparatorDimensionalCut cdc3 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
            cdc3.setComparator(comparatorR);
            cdc3.setDimensionalCut(dimensionalcut3);
            cdc3.setRegisterDate(date);
            num=ScoreCardUtils.createRandomNumber();
            realRoot+=num;
            cdc3.setRegister(num);
            delegate.createComparatorDimensionalCut(cdc3);

            ComparatorDimensionalCut cdc4 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
            cdc4.setComparator(comparatorM);
            cdc4.setDimensionalCut(dimensionalcut3);
            cdc4.setRegisterDate(date);
            num=ScoreCardUtils.createRandomNumber();
            goalRoot+=num;
            cdc4.setRegister(num);
            delegate.createComparatorDimensionalCut(cdc4);


            //Populate ComparatorDimensionalCut - TOTAL REGION

            ComparatorDimensionalCut cdc = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
            cdc.setComparator(comparatorR);
            cdc.setDimensionalCut(dimensionalcut1);
            cdc.setRegisterDate(date);
            cdc.setRegister(realRoot);
            delegate.createComparatorDimensionalCut(cdc);

            ComparatorDimensionalCut cdc2 = (ComparatorDimensionalCut) Factory.getBean("ComparatorDimensionalCut");
            cdc2.setComparator(comparatorM);
            cdc2.setDimensionalCut(dimensionalcut1);
            cdc2.setRegisterDate(date);
            cdc2.setRegister(goalRoot);
            delegate.createComparatorDimensionalCut(cdc2);

        }




        //Create ScorecardRelation
        ScoreCardRelation scoreCardRelation1 = (ScoreCardRelation) Factory.getBean("ScoreCardRelation");
        scoreCardRelation1.setScoreCard(mapScorecard);
        scoreCardRelation1.setOtherScoreCard(perspectiveScorecard);
        scoreCardRelation1.setPercentFactor(1D);
        delegate.createScoreCardRelation(scoreCardRelation1);

        ScoreCardRelation scoreCardRelation2 = (ScoreCardRelation) Factory.getBean("ScoreCardRelation");
        scoreCardRelation2.setScoreCard(perspectiveScorecard);
        scoreCardRelation2.setOtherScoreCard(objectiveScorecard);
        scoreCardRelation2.setPercentFactor(1D);
        delegate.createScoreCardRelation(scoreCardRelation2);

        ScoreCardRelation scoreCardRelation3 = (ScoreCardRelation) Factory.getBean("ScoreCardRelation");
        scoreCardRelation3.setScoreCard(objectiveScorecard);
        scoreCardRelation3.setOtherScoreCard(pointer);
        scoreCardRelation3.setPercentFactor(1D);
        delegate.createScoreCardRelation(scoreCardRelation3);

        //request.setAttribute("mapScorecard", mapScorecard);

        return new ModelAndView("advancePopulate");
    }
}
